package com.example.demo.lockAndSycn;

/**
 * 创建资源类, 在资源类创建属性和操作方法
 */
class Ticket_sync {
    private static Integer NUMBER = 30;

    public synchronized static void saleTicket() {
        if (NUMBER > 0) {
            System.out.println(Thread.currentThread().getName() + "正在买" + (NUMBER--) + "票" + ",剩下: " + NUMBER);
        } else {
            System.out.println("没票了");
        }
    }
}

public class ThreadDemo_sycn {

    public static void main(String[] args) {
        new Thread(() -> {
            for (int i = 0; i < 15; i++) {
                Ticket_sync.saleTicket();
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                Ticket_sync.saleTicket();
            }
        }, "BB").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                Ticket_sync.saleTicket();
            }
        }, "CC").start();
    }
}
